package com.zjdiepu.www.biz.partner;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.ThirdBusiStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.partner.ThRepaymentMapper;
import com.zjdiepu.www.dao.partner.ThYwBusinessMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.partner.IThRepaymentService;
import com.zjdiepu.www.model.partner.dto.ThRepayment;
import com.zjdiepu.www.model.partner.dto.ThYwBusiness;
import com.zjdiepu.www.model.partner.ext.ThRepaymentExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;

/**
 * 第三方还款信息Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年09月28日 10时13分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThRepaymentServiceImpl extends BaseService implements IThRepaymentService {
	public static final Logger logger = LoggerFactory.getLogger(ThRepaymentServiceImpl.class);

	@Autowired
	private ThRepaymentMapper thRepaymentMapper;
	@Autowired
	private ThYwBusinessMapper thYwBusinessMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	
	@Override
	public void saveUpdateAndDeleteAll(ThRepayment thRepayment, OperateType add) {
		thRepayment.initInsertData(thRepayment.getModifier(), thRepayment.getModifierName());
		thRepaymentMapper.insert(thRepayment);
		
		//更新第三方订单信息
		ThYwBusiness thYwBusiness=new ThYwBusiness();
		thYwBusiness.setBusiId(thRepayment.getBusiId());;
		thYwBusiness=thYwBusinessMapper.selectRefined(thYwBusiness);
		if(thYwBusiness!=null){
			thYwBusiness.initUpdateData(thRepayment.getModifier(), thRepayment.getModifierName());
			thYwBusiness.setThirdBusiStatus(ThirdBusiStatus.YI_HUAN_KUAN.getCode());
			thYwBusiness.setDateRepayment(thRepayment.getDateRepayment());
			thYwBusiness.setActualRaiseFee(thRepayment.getAmountRepaymen());
			thYwBusinessMapper.updateByPrimaryKeySelective(thYwBusiness);
		}
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(thRepayment.getBusiId());
		if(ywBusiness!=null){
			ywBusiness.setThirdBusiStatus(ThirdBusiStatus.YI_HUAN_KUAN.getCode());
			ywBusiness.initUpdateData(thRepayment.getModifier(), thRepayment.getModifierName());
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		}
		
	}
	
	@Override
	public void saveUpdateAndDelete(ThRepayment thRepayment, OperateType operType) {
		switch (operType) {
		case add:
			thRepayment.initInsertData(thRepayment.getModifier(), thRepayment.getModifierName());
			thRepaymentMapper.insert(thRepayment);
			break;
		case edit:
			thRepayment.initUpdateData(thRepayment.getModifier(), thRepayment.getModifierName());
			// 影响的记录数
			int updateCount = thRepaymentMapper.updateByPrimaryKeySelective(thRepayment);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thRepayment.initDeleteData(thRepayment.getModifier(), thRepayment.getModifierName());
			// 影响的记录数
			int deleteCount = thRepaymentMapper.updateByPrimaryKeySelective(thRepayment);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThRepayment> selectByExt(ThRepaymentExt thRepaymentExt) {
		return thRepaymentMapper.selectByExt(thRepaymentExt);
	}
	
	@Override
	public int countByExt(ThRepaymentExt thRepaymentExt){
		return thRepaymentMapper.countByExt(thRepaymentExt);
	}
	
	@Override
	public ThRepayment selectById(String id) {
		return thRepaymentMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thRepaymentMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThRepayment thRepayment) {
		return thRepaymentMapper.countRefined(thRepayment);
	}

	@Override
	public ThRepayment selectRefined(ThRepayment thRepayment) {
		return thRepaymentMapper.selectRefined(thRepayment);
	}

	@Override
	public boolean repeat(ThRepayment thRepayment) {
		int count = countRefined(thRepayment);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thRepayment.getId())) {
			return true;
		}
		ThRepayment dbItem = selectRefined(thRepayment);
		if(null == dbItem) {
			return false;
		}
		return !thRepayment.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thRepaymentMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	
}
